﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Paros.BLL;
using Paros.Domain;
namespace Paros.Win
{
    public partial class ImportIEForm : BaseForm
    {
        EntryMgr entryMgr = new EntryMgr();
        CategoryMgr cateMgr = new CategoryMgr();
        public ImportIEForm()
        {
            InitializeComponent();
        }

        private void ImportIEForm_Load(object sender, EventArgs e)
        {
            CategoryControl control = new CategoryControl();
            control.Dock = DockStyle.Fill;
            panel2.Controls.Add(control);
        }

        private void btImport_Click(object sender, EventArgs e)
        {
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

            ReWork(folder, 0);
        }

        private void ReWork(string folderName, int parentId)
        {
            DirectoryInfo root = new DirectoryInfo(folderName);
   
            foreach (FileInfo file in root.GetFiles())
            {
                if (file.Extension.ToUpper() == ".URL")
                {
                    using (StreamReader reader = file.OpenText())
                    {
                        entryMgr.InsertEntry(parentId, file.Name, reader.ReadToEnd(), "", DateTime.Now);
                    }
                    
                }
            }

            int cateId = -1;
            foreach (DirectoryInfo dir in root.GetDirectories())
            {
                cateId = cateMgr.InsertCategory(parentId, dir.Name);
                ReWork(dir.FullName, cateId);
            }
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
